package info.monkeyws.AutoRinger;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarPreference extends DialogPreference {
	public int MAX_VALUE = 30;
	public int MIN_VALUE = 1;
	public int DEFAULT_VALUE = 6; 
	
	private int time_interval;
	private SeekBar seekBar;
	private TextView textView;
	private Context context;
	
	public SeekBarPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;		
	}
	
	@Override
	protected void onPrepareDialogBuilder(Builder builder) {
		// TODO Auto-generated method stub
		
		LinearLayout layout=new LinearLayout(context);
	    layout.setPadding(10, 0, 10, 0);
	    layout.setOrientation(1);
	    
	    textView=new TextView(context);
	    	    
	    seekBar=new SeekBar(context);
	    seekBar.setMax(MAX_VALUE - MIN_VALUE);
	    seekBar.setOnSeekBarChangeListener(listener);
	    time_interval = getPersistedInt(DEFAULT_VALUE);
	    seekBar.setProgress(time_interval - MIN_VALUE);
	    	    
	    textView.setText(Integer.toString(time_interval));
	    layout.addView(seekBar);
	    layout.addView(textView);
	     
	    builder.setView(layout);
	
		super.onPrepareDialogBuilder(builder);
	}
	
	OnSeekBarChangeListener listener = new OnSeekBarChangeListener(){

		@Override
		public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
			// TODO Auto-generated method stub
			time_interval = seekBar.getProgress() + MIN_VALUE;
			textView.setText(Integer.toString(time_interval));
		}

		@Override
		public void onStartTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onStopTrackingTouch(SeekBar arg0) {
			// TODO Auto-generated method stub
			
		}
		
	};

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		// TODO Auto-generated method stub
		if (positiveResult){
			persistInt(time_interval);
		}
		super.onDialogClosed(positiveResult);
	}

}